package HomeService

import (
	"context"
	"gdshop-font-api-go/app/entity"
	"gdshop-font-api-go/app/request/HomeReq"
	"gdshop-font-api-go/app/response/DiyPageResp"
	"gdshop-font-api-go/library/response"
	toolsDb "gdshop-font-api-go/library/tools/db"
)

func Preview(ctx context.Context, parames *HomeReq.Preview) *response.JsonResponse {
	diyPageItemList := []*entity.DiyPageItem{}
	err := toolsDb.GetUnSafaTable(ctx, "diy_page_item").As("dpi").
		LeftJoin("diy_page dp", "dp.id = dpi.page_id").
		Where("dp.page_type = 1 AND dp.status = 1 AND dp.delete_at < 1 AND dpi.delete_at < 1 AND dpi.page_id = ?",
			parames.Id).
		Fields("dpi.*").Order("dpi.sort DESC").Structs(&diyPageItemList)
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	result := []*DiyPageResp.Result{}
	// 解析jsonData
	for _, item := range diyPageItemList {
		result = append(result, &DiyPageResp.Result{
			TypeId: item.TypeId,
			Data:   parsingJsonData(ctx, item.TypeId, item.JsonData),
		})
	}

	return response.SuccessByRequestMessageData(nil, "成功", result)
}
